package com.bt.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bt.annotation.RequiresPermissionsDesc;
import com.bt.pojo.*;
import com.bt.service.*;
import com.bt.util.ResponseUtil;
import com.bt.vo.CatVo;
import com.bt.vo.DtsBrandVo;
import com.bt.vo.GoodsAllinone;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.tags.Param;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/23 15:10
 **/
@CrossOrigin
@RequestMapping("/admin/goods")
@RestController
public class AdminGoodsController {
    @Autowired
    private DtsGoodsService dtsGoodsServiceImpl;
    @Autowired
    private DtsCategoryService dtsCategoryServiceImpl;
    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;
    @Autowired
    private DtsGoodsAttributeService dtsGoodsAttributeService;
    @Autowired
    private DtsGoodsSpecificationService dtsGoodsSpecificationService;
    @Autowired
    private DtsBrandService dtsBrandServiceImpl;

    /**
     * http://localhost:8080/admin/goods/list?page=1&limit=20&goodsSn=11&name=00&sort=add_time&order=desc
     * get 查询商品列表
     */
    @GetMapping("/list")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "查询")
    @RequiresPermissions("admin:goods:list")
    public Object list(Integer page,Integer limit,String goodsSn,String name,String sort,String order){
        IPage<DtsGoods> Ipage=dtsGoodsServiceImpl.findDTsGoodsByPage(page,limit,goodsSn,name,sort,order);
        Map data=new HashMap();
        data.put("total",Ipage.getTotal());
        data.put("items",Ipage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/goods/detail?id=23852
     * get
     */
    @RequiresPermissions("admin:goods:detai")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "详情")
    @GetMapping("/detail")
    public Object detail(@RequestParam("id") Integer Id){
        if (Id==null){
            return ResponseUtil.badArgument();
        }

//        查询categoryIds
       List<Integer> categoryIds=dtsGoodsServiceImpl.findCategoryIdsById(Id);
       List<DtsGoodsAttribute> attributes=dtsGoodsAttributeService.findDtsGoodsAttributeByGoodsId(Id);
       List<DtsGoodsProduct> products=dtsGoodsProductService.findDtsGoodsProductByGoodsId(Id);
       List<DtsGoodsSpecification> specifications=dtsGoodsSpecificationService.findDtsGoodsSpecificationByGoodsId(Id);
       DtsGoods goods=dtsGoodsServiceImpl.findDtsGoodById(Id);
        GoodsAllinone goodsAllinone=new GoodsAllinone();
        goodsAllinone.setAttributes(attributes);
        goodsAllinone.setCategoryIds(categoryIds);
        goodsAllinone.setProducts(products);
        goodsAllinone.setSpecifications(specifications);
        goodsAllinone.setGoods(goods);
        return ResponseUtil.ok(goodsAllinone);
    }
    /**
     * http://localhost:8080/admin/goods/catAndBrand
     * get
     */
    @RequiresPermissions("admin:goods:catAndBrand")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "获取商品分类和品牌")
    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
        List<CatVo> catVos=dtsCategoryServiceImpl.findCatVos();
        List<DtsBrandVo> brands=dtsBrandServiceImpl.findDtsBrandVos();
        Map data=new HashMap();
        data.put("categoryList",catVos);
        data.put("brandList",brands);
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/goods/update
     * post 更新商品
     */
    @RequiresPermissions("admin:goods:update")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "更新")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        try {
            dtsGoodsServiceImpl.updateGoods(goodsAllinone);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(999,e.getMessage());
        }
        return ResponseUtil.ok();
    }
   /**
    * http://localhost:8080/admin/goods/create
    *  post
    */
    @RequiresPermissions("admin:goods:create")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "创建")
    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        try {
            dtsGoodsServiceImpl.createGoods(goodsAllinone);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(999,e.getMessage());
        }
        return ResponseUtil.ok();
    }
    /**
     * http://localhost:8080/admin/goods/delete
     * post
     */
    @RequiresPermissions("admin:goods:delete")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){
        Integer id=goods.getId();
        if (id==null){
            return ResponseUtil.badArgument();
        }
        try {
            dtsGoodsServiceImpl.deleteGoodsById(id);
            return ResponseUtil.ok();
        } catch (Exception e) {
            return ResponseUtil.fail(999,e.getMessage());
        }
    }

}
